'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class UserInfo
    Implements ISerializable

    Private _DataSize As Integer
    Private _HasData As Boolean

    Public ReadOnly Property DataSize() As Integer Implements ISerializable.DataSize
        Get
            Return _DataSize
        End Get
    End Property

    Public ReadOnly Property TotalSize() As Integer Implements ISerializable.TotalSize
        Get
            Return _DataSize
        End Get
    End Property

    Public ReadOnly Property HasData() As Boolean Implements ISerializable.HasData
        Get
            Return _HasData
        End Get
    End Property

    Public Function CalculateDataSize() As Integer Implements ISerializable.CalculateDataSize
        Return _UserClass.CalculateDataSize + _DCInfo.CalculateDataSize + _ExternalAddress.CalculateDataSize + _UserStatus.CalculateDataSize + _UserCapabilities.CalculateDataSize + _OnlineTime.CalculateDataSize + _SignOnTime.CalculateDataSize + _MemberSince.CalculateDataSize + _UserIconIdAndHash.CalculateDataSize
    End Function

    Public Function CalculateTotalSize() As Integer Implements ISerializable.CalculateTotalSize
        Return CalculateDataSize()
    End Function

    Private _Uin As String
    Public Property Uin() As String
        Get
            Return _Uin
        End Get
        Set(ByVal value As String)
            _Uin = value
        End Set
    End Property

    Private _WarningLevel As Integer
    Public Property WarningLevel() As Integer
        Get
            Return _WarningLevel
        End Get
        Set(ByVal value As Integer)
            _WarningLevel = value
        End Set
    End Property

    Private _UserClass As New TlvUserClass
    Public ReadOnly Property UserClass() As TlvUserClass
        Get
            Return _UserClass
        End Get
    End Property

    Private _UserStatus As New TlvUserStatus
    Public ReadOnly Property UserStatus() As TlvUserStatus
        Get
            Return _UserStatus
        End Get
    End Property

    Private _DCInfo As New TlvDCInfo
    Public ReadOnly Property DCInfo() As TlvDCInfo
        Get
            Return _DCInfo
        End Get
    End Property

    Private _ExternalAddress As New TlvExternalIpAddress
    Public ReadOnly Property ExternalAddress() As TlvExternalIpAddress
        Get
            Return _ExternalAddress
        End Get
    End Property

    Private _SignOnTime As New TlvSignonTime
    Public ReadOnly Property SignOnTime() As TlvSignonTime
        Get
            Return _SignOnTime
        End Get
    End Property

    Private _MemberSince As New TlvMemberSince
    Public ReadOnly Property MemberSince() As TlvMemberSince
        Get
            Return _MemberSince
        End Get
    End Property

    Private _UserCapabilities As New TlvCapabilities
    Public ReadOnly Property UserCapabilities() As TlvCapabilities
        Get
            Return _UserCapabilities
        End Get
    End Property

    Private _OnlineTime As New TlvOnlineTime
    Public ReadOnly Property OnlineTime() As TlvOnlineTime
        Get
            Return _OnlineTime
        End Get
    End Property

    Private _UserIconIdAndHash As New TlvUserIconIdAndHash
    Public ReadOnly Property UserIconIdAndHash() As TlvUserIconIdAndHash
        Get
            Return _UserIconIdAndHash
        End Get
    End Property

    Public Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte)) Implements ISerializable.Deserialize
        Dim index As Integer = 0

        If data(index) = &H0 And data(index + 1) = &H6 Then index += 8

        _Uin = ByteConverter.ToStringFromByteIndex(index, data)
        index += 1 + _Uin.Length

        _WarningLevel = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        Dim innerTlvCount As Integer
        Dim innerTlvIndex As Integer

        innerTlvCount = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        Do While innerTlvIndex < innerTlvCount
            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &H1
                    _UserClass.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &HC
                    _DCInfo.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &HA
                    _ExternalAddress.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H6
                    _UserStatus.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &HD
                    _UserCapabilities.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &HF
                    _OnlineTime.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H3
                    _SignOnTime.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H5
                    _MemberSince.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H1D
                    _UserIconIdAndHash.Deserialize(data.GetRange(index, desc.TotalSize))
            End Select

            index += desc.TotalSize
            innerTlvIndex += 1
        Loop

        _DataSize = index

        _HasData = True
    End Sub

    Public Function Deserialize(ByVal offset As Integer, ByVal data As List(Of Byte)) As Integer
        Deserialize(data.GetRange(offset, data.Count - offset))

        Return DataSize
    End Function

    Public Function Serialize() As System.Collections.Generic.List(Of Byte) Implements ISerializable.Serialize
        Dim data As List(Of Byte)

        data = New List(Of Byte)

        data.AddRange(_UserClass.Serialize)
        data.AddRange(_DCInfo.Serialize)
        data.AddRange(_ExternalAddress.Serialize)
        data.AddRange(_UserStatus.Serialize)
        data.AddRange(_UserCapabilities.Serialize)
        data.AddRange(_OnlineTime.Serialize)
        data.AddRange(_SignOnTime.Serialize)
        data.AddRange(_MemberSince.Serialize)
        data.AddRange(_UserIconIdAndHash.Serialize)

        Return data
    End Function

End Class